﻿using Entities;
using Mapster;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using Utils;
using VO.Project;

namespace Bridges
{
    [ClassInterface(ClassInterfaceType.None)]
    [ComVisible(true)]
    public class Project
    {
        /// <summary>
        /// 项目列表
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public string List(string data)
        {
            ListVO vo = JsonConvert.DeserializeObject<ListVO>(data);
            int total = 0;
            List<ProjectEntity> projects = SqlSugarHelper.Db.Queryable<ProjectEntity>()
                .Where(pe => pe.IsActive == 1)
                .WhereIF(!string.IsNullOrEmpty(vo.Content), pe => pe.Name.Contains(vo.Content))
                .Mapper(pe => {
                    pe.User = SqlSugarHelper.Db.Queryable<UserEntity>().InSingle(pe.UId);
                    pe.Users = SqlSugarHelper.Db.Queryable<UserEntity>().Where(ue => pe.UIds.Contains(ue.Id)).ToList();
                })
                .OrderByDescending(pe => pe.Id)
                .ToPageList(vo.PageIndex, vo.PageSize, ref total);

            return JsonConvert.SerializeObject(new {
                StatusCode = 200,
                Succeeded = true,
                Data = projects,
                Errors = "",
                Extras = new { Total = total },
                Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            }, new JsonSerializerSettings { DateFormatString = "yyyy-MM-dd HH:mm:ss" });
        }

        /// <summary>
        /// 增加项目
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public string Save(string data)
        {
            SaveVO vo = JsonConvert.DeserializeObject<SaveVO>(data);

            SqlSugarHelper.Db.Insertable<ProjectEntity>(vo.Adapt<ProjectEntity>()).ExecuteCommand();

            return JsonConvert.SerializeObject(new
            {
                StatusCode = 200,
                Succeeded = true,
                Data = "",
                Errors = "",
                Extras = new { },
                Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            }, new JsonSerializerSettings { DateFormatString = "yyyy-MM-dd HH:mm:ss" });
        }

        /// <summary>
        /// 删除项目
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public string Delete(string data)
        {
            DeleteVO vo = JsonConvert.DeserializeObject<DeleteVO>(data);

            SqlSugarHelper.Db.Updateable<ProjectEntity>().SetColumns(u => u.IsActive == 0).Where(u => u.Id == vo.Id).ExecuteCommand();

            return JsonConvert.SerializeObject(new
            {
                StatusCode = 200,
                Succeeded = true,
                Data = "",
                Errors = "",
                Extras = new { },
                Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            }, new JsonSerializerSettings { DateFormatString = "yyyy-MM-dd HH:mm:ss" });
        }

        /// <summary>
        /// 修改项目
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public string Update(string data)
        {
            UpdateVO vo = JsonConvert.DeserializeObject<UpdateVO>(data);

            SqlSugarHelper.Db.Updateable<ProjectEntity>(vo).ExecuteCommand();

            return JsonConvert.SerializeObject(new
            {
                StatusCode = 200,
                Succeeded = true,
                Data = "",
                Errors = "",
                Extras = new { },
                Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            }, new JsonSerializerSettings { DateFormatString = "yyyy-MM-dd HH:mm:ss" });
        }

        /// <summary>
        /// 查看项目
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public string Show(string data)
        {
            ShowVO vo = JsonConvert.DeserializeObject<ShowVO>(data);

            ProjectEntity project = SqlSugarHelper.Db.Queryable<ProjectEntity>()
                .Where(pe => pe.IsActive == 1)
                .Mapper(pe => {
                    pe.User = SqlSugarHelper.Db.Queryable<UserEntity>().InSingle(pe.UId);
                    pe.Users = SqlSugarHelper.Db.Queryable<UserEntity>().Where(ue => pe.UIds.Contains(ue.Id)).ToList();
                })
                .InSingle(vo.Id);

            return JsonConvert.SerializeObject(new
            {
                StatusCode = 200,
                Succeeded = true,
                Data = project,
                Errors = "",
                Extras = new { },
                Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            }, new JsonSerializerSettings { DateFormatString = "yyyy-MM-dd HH:mm:ss" });
        }

        /// <summary>
        /// 添加成员
        /// </summary>
        /// <returns></returns>
        public string AddUser(string data) {
            AddUserVO vo = JsonConvert.DeserializeObject<AddUserVO>(data);

            SqlSugarHelper.Db.Updateable<ProjectEntity>(new { UIds = vo.UIds })
                .Where(pj => pj.Id == vo.Id)
                .ExecuteCommand();

            ProjectEntity project = SqlSugarHelper.Db.Queryable<ProjectEntity>()
               .Mapper(pe => {
                   pe.User = SqlSugarHelper.Db.Queryable<UserEntity>().InSingle(pe.UId);
                   pe.Users = SqlSugarHelper.Db.Queryable<UserEntity>().Where(ue => pe.UIds.Contains(ue.Id)).ToList();
               })
               .InSingle(vo.Id);

            return JsonConvert.SerializeObject(new
            {
                StatusCode = 200,
                Succeeded = true,
                Data = project,
                Errors = "",
                Extras = new { },
                Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            }, new JsonSerializerSettings { DateFormatString = "yyyy-MM-dd HH:mm:ss" });
        }
    }
}
